package cfs.formbean;

import java.util.ArrayList;
import java.util.List;

import org.mybeans.form.FormBean;

public class SellFundFormBean extends FormBean {
	private String selectedFund;
	private String numberOfShares;

	public String getSelectedFund() {
		return selectedFund;
	}

	public void setSelectedFund(String selectedFund) {
		this.selectedFund = selectedFund;
	}

	public String getNumberOfShares() {
		return numberOfShares;
	}

	public void setNumberOfShares(String numberOfShares) {
		this.numberOfShares = numberOfShares;
	}

	public List<String> getValidationErrors() {
		List<String> errors = new ArrayList<String>();

		if (numberOfShares == null || numberOfShares.length() == 0) {
			errors.add("Share amount should not be empty");
		}

                if (selectedFund == null || selectedFund.length() == 0) {
			errors.add("You must select a fund");
		}
		// basic check
		if (errors.size() > 0)
			return errors;

		// further check
		try {
			double amountDollar = Double.parseDouble(numberOfShares);
			if (amountDollar <= 0) {
				errors.add("Please enter a positive amount");
			}
			int index = numberOfShares.lastIndexOf('.');
			if (index != -1 && numberOfShares.length() - index > 4) {
				errors.add("You can not specify more the three decimal");
			}
		} catch (NumberFormatException n) {
			errors.add("Please enter a valid number for this fund");
		}

		return errors;
	}

}
